Write a C# program to generate the Fibonacci sequence up to a given number n.
The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones.
home / developersection / forums / fibonacci sequence
Write a C# program to generate the Fibonacci sequence up to a given number n.
The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones.
Ashutosh Kumar Verma
13-Jun-2024Fibonacci Sequence in C#
Below is an example of how you can generate the Fibonacci sequence using an iterative approach in C#
Example-
In the above example-
We determine how many elements in Fibonacci numbers to print (
maxNumber).We define the function
Fibonaccito calculate the nth Fibonacci number.In the
Fibonaccifunction, we start with the first two Fibonacci numbers,aandb.Then we iterate from the third Fibonacci number to the nth Fibonacci number, counting each Fibonacci number several times.
Finally, we iterate the loop in
Mainmethod for themaxNumbertimes to generate the Fibonacci numbers as you need.Output-
Also, Read: Pascal Triangle